package org.ithang.lock;

import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;
/**
 * 售票示例，采用synchronized关键字
 * @author Administrator
 *
 */
public class ThreadLockTest {

	
	private int count=100;
	
	@Test
	public void ticketTest(){
		
		new Thread(()->sell(),"1号售票员").start();
		new Thread(()->sell(),"2号售票员").start();
		new Thread(()->sell(),"3号售票员").start();
		
		try{
		    Thread.sleep(50000);
		}catch(Exception e){}
	}
	
	
	private void sell(){
		while(count>0){
			synchronized(this) {
			    System.out.println(Thread.currentThread().getName()+" 卖出第"+count+"张票");
			    count--;
			}
			try{
				
			    Thread.sleep(1000);
			}catch(Exception e){}
			
		}
	}
	
	
}
